<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
use DB;

class Worklink extends Model
{
    protected $table = 'work_link';
    protected $primaryKey = 'id';

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public static function workLinkDetial($kfid)
    {
        return self::where("kfid",$kfid)->first();
    }

    public static function workLinkDetials($kfid)
    {
        return self::select('work_link.*','work_qrcode.switch','work_qrcode.upper','work_qrcode.daily','work_qrcode.device_bind')->leftjoin('work_qrcode','work_qrcode.kfid','=','work_link.kfid')->where("work_link.kfid",$kfid)->first();
    }

    public static function getWorkLinkByKfid($kfid)
    {
        return self::where("kfid",$kfid)->where("disable",1)->first();
    }

    public static function userWorkLinks($users_id)
    {
        return self::where("users_id",$users_id)->where("disable",1)->orderBy("id","DESC")->paginate(25);
    }

    public static function createWorkLink($kfid,$users_id,$corp_id,$remark,$kefu_nike,$kefu_head_img,$limit_pv,$limit_time,$open_kfid,$link_url)
    {
        return self::insert(["kfid"=>$kfid,"users_id"=>$users_id,"corp_id"=>$corp_id,"remark"=>$remark,"kefu_nike"=>$kefu_nike,"kefu_head_img"=>$kefu_head_img,"limit_pv"=>$limit_pv,"limit_time"=>$limit_time,"limit_left"=>$limit_pv,"open_kfid"=>$open_kfid,"link_url"=>$link_url,"created_at"=>time()]);
    }

    public static function updateWorkLink($kfid,$corp_id,$remark,$kefu_nike,$kefu_head_img,$open_kfid,$link_url)
    {
        return self::where("kfid",$kfid)->update(["corp_id"=>$corp_id,"remark"=>$remark,"kefu_nike"=>$kefu_nike,"kefu_head_img"=>$kefu_head_img,"open_kfid"=>$open_kfid,"link_url"=>$link_url]);
    }

    public static function getUserWorkLinkCrop($users_id,$corp_id)
    {
        return self::where("users_id",$users_id)->where("corp_id",$corp_id)->where("disable",1)->first();
    }

    public static function workLinkDelect($kfid)
    {
        return self::where("kfid",$kfid)->update(["disable"=>2]);
    }

    public static function updateWorkLinkRemark($kfid,$remark)
    {
        return self::where("kfid",$kfid)->update(["remark"=>$remark]);
    }

    public static function updateWorkLinkLimitPv($kfid,$limit_left)
    {
        return self::where("kfid",$kfid)->decrement("limit_left", $limit_left);
    }

    public static function updateWorkLinkMember($users_id,$limit_pv,$limit_time)
    {
        return self::where("users_id",$users_id)->where("disable",1)->update(["limit_pv"=>$limit_pv,"limit_left"=>$limit_pv,"limit_time"=>$limit_time]);
    }

    public static function cardWorkLinkSample($users_id)
    {
        return self::select("kfid","remark")->where("users_id",$users_id)->where("disable",1)->take(200)->get();
    }

    public static function userWorkLinkLists($users_id)
    {
        return self::select("kfid")->where("users_id",$users_id)->where("disable",1)->get();
    }
}
